﻿using UnityEngine;
using UnityEngine.Extension;
using UnityEditor;
using UnityEditor.Extension;
using System.Collections.Generic;
using System.Extension;
using System;

[CustomPropertyDrawer(typeof(ControlSubject))]
public class ControlSubjectPropertyDrawer : PropertyDrawer 
{
    static float lineHeight = 16;
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {

        PropertyIndexer iProperty = new PropertyIndexer(property);
        SubjectiveScrollList scrollList = property.GetComponentInParent<SubjectiveScrollList>();

        bool hasContent = iProperty["content"].objectReferenceValue || scrollList;
        Rect pos = new Rect(position);  pos.height = lineHeight;
        
        //Content
        EditorGUI.PropertyField(pos, iProperty["content"], label);
        if (hasContent)
        {
            List<string> links = GetLinks(iProperty);
            int index = links.IndexOf(iProperty["memberPath"].stringValue);
            int indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel = 1;

            pos.y += lineHeight;
            EditorGUI.PropertyField(pos, iProperty["state"]);
            
            pos.y += lineHeight;
            index = EditorGUI.Popup(pos, "Memberpath", index, links.ToArray());
            if (index > -1) iProperty["memberPath"].stringValue = links[index] == "NONE" ? string.Empty : links[index];

            pos.y += lineHeight;
            //EditorGUI.PropertyField(pos, iProperty["localize"]);
            iProperty["localize"].boolValue = EditorGUI.Toggle(pos, "Localize", iProperty["localize"].boolValue);
            EditorGUI.indentLevel = indent;
        }
    }
    
    List<Type> GetTypes(SerializedProperty property)
    {
        List<Type> types = new List<Type>();
        property.ForEachItem("", t =>
        {
            types.Add(TypeExtension.GetType(t.stringValue));
            return false;
        });
        return types;
    }

    List<string> GetLinks(PropertyIndexer property)
    {
        List<Type> types = GetTypes(property["types"]);
        List<string> mlinks = new List<string>();
        SubjectiveScrollList scrollList = property.property.GetComponentInParent<SubjectiveScrollList>();

        if (property["content"].objectReferenceValue.isNotNone() )
        {
            mlinks = property["content"].objectReferenceValue.MemberPathsInclusiveTypes(
           property["genericType"].boolValue, "", types.ToArray());
        }
        else if (scrollList)
        {
            if (scrollList.subject.hasContent)
            {
                Type valueType = scrollList.subject.value.GetType();
                string typeName = valueType.ToString().GetStringBetween("[", "]");
                valueType = TypeExtension.GetType(typeName);
                mlinks = valueType.MemberPathsFromType(property["genericType"].boolValue,
                    "", types.ToArray());
            }
        }
        mlinks.Insert(0, "NONE");
        return mlinks;
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        SubjectiveScrollList scrollList = property.GetComponentInParent<SubjectiveScrollList>();
        PropertyIndexer iProperty = new PropertyIndexer(property);
        bool hasContent = iProperty["content"].objectReferenceValue || scrollList;
        return hasContent ? 4 * lineHeight : lineHeight;
    }
}
